﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EA.DTO;

namespace EA.DAO
{
    public class MemberDA : ObjectDA
    {

        public MemberDA(String connection_string)
            : base(connection_string)
        {

        }

        public DataTable GetMemberByUsername(String username)
        {
            String query = "SELECT Member.* FROM Member WHERE username=@username";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@username", username);
            DataTable table = new DataTable();
            this.Fill(query, param, table);
            return table;
        }

        public DataTable GetListMember()
        {
            String query = "SELECT  * FROM Member";
            DataTable table = new DataTable();
            this.Fill(query, null, table);
            return table;
        }

        public DataTable GetMemberByID(int ID)
        {
            String query = "SELECT * FROM Member WHERE ID=@ID";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@ID", ID);
            DataTable table = new DataTable();
            this.Fill(query, param, table);
            return table;
        }

        public Boolean DeleteMember(int ID)
        {
            String query = "DELETE Member WHERE ID=@ID";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@ID", ID);
            return this.ExecuteNonQuery(query, param);
        }

        public Boolean UpdateMember(MemberTO member)
        {
            String query = "UPDATE Member SET FullName=@fullName,Password=@password,Role=@role WHERE ID=@ID";
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("ID", member.ID);
            param[1] = new SqlParameter("fullName", member.FullName);
            param[2] = new SqlParameter("password", member.Password);
            param[3] = new SqlParameter("role", member.Role);
            return this.ExecuteNonQuery(query, param);
        }

        public Boolean InsertMember(MemberTO member)
        {
            String query = "INSERT INTO Member(UserName,Password,FullName,Role,LastLogOnDate) VALUES(@UserName,@Password,@FullName,@Role,@LastLogOnDate)";
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@UserName", member.UserName);
            param[1] = new SqlParameter("@Password", member.Password);
            param[2] = new SqlParameter("@FullName", member.FullName);
            param[3] = new SqlParameter("@Role", member.Role);
            param[4] = new SqlParameter("@LastLogOnDate", member.LastLogOnDate);
            return this.ExecuteNonQuery(query, param);
        }

        public Boolean UpdateLastLogOnDate(int ID)
        {
            String query = "UPDATE Member SET LastLogOnDate=@LastLogOnDate WHERE ID=@ID";
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("ID", ID);
            param[1] = new SqlParameter("LastLogOnDate", DateTime.Now);
            return this.ExecuteNonQuery(query, param);
        }
    }
}
